package development.socket;

import state.AlertListener;
import state.GameAlert;
import state.GameGlobal;
import state.socket.DataPackage;
import development.FrmLogin;
import development.Global;
import development.app.FrmMangXH;
import development.app.mxh.FrmMessage;
import development.bussiness.AuthenticateBussiness;
import development.bussiness.AvatarBussiness;
import development.bussiness.ChargingBussiness;
import development.bussiness.FriendBussiness;
import development.bussiness.NotificationBusiness;
import development.bussiness.RankingBussiness;
import development.bussiness.SubscriptionBussiness;

public class AuthenticateHandle extends RequestGroupHandle implements AlertListener {
  public AuthenticateBussiness authenticateBussiness;
  private RankingBussiness rankingBussiness;
  private NotificationBusiness notificationBussiness;
  private ChargingBussiness chargingBussiness;
  private AvatarBussiness avatarBussiness;
  public FriendBussiness friendBussiness;
  public SubscriptionBussiness subscriptionBussiness;
  
  public AuthenticateHandle() {
    authenticateBussiness = new AuthenticateBussiness();
    rankingBussiness = new RankingBussiness();
    notificationBussiness = new NotificationBusiness();
    chargingBussiness = new ChargingBussiness();
    avatarBussiness = new AvatarBussiness();
    friendBussiness = new FriendBussiness();
    subscriptionBussiness = new SubscriptionBussiness();
  }
  
  public void onConnectFail() {
    super.onConnectFail();
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    if (alertId == 99) {
      GameGlobal.nextState(FrmLogin.instance(), null);
    }
  }

  public void processPackage(DataPackage dataPackage) {
    super.processPackage(dataPackage);
    int header = dataPackage.getHeader();
    switch (header) {

    /** Check for update */
    case ProtocolConstants.ResponseHeader.UPDATE_PROVIDER_RESPONSE:
      authenticateBussiness.onUpdateProviderResponse(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.UPDATE_CLIENT_RESPONSE:
      authenticateBussiness.onUpdateClienResponse(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.NO_UPDATE_RESPONSE:
      authenticateBussiness.onNoUpdateResponse(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.NOTIFICATION_RESPONSE:
      notificationBussiness.onReceiveNotification(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.COIN_CHARGING_SUCCESS_RESPONSE:
      chargingBussiness.onCoinChargingSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.COIN_CHARGING_FAIL_RESPONSE:
      chargingBussiness.onCoinChargingFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.DOWNLOAD_URL_RESPONSE:
      authenticateBussiness.onDownloadUrlReceive(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.UPDATE_GAME_VERSION_INFO_RESPONSE:
      authenticateBussiness.onUpdateGameVersionInfo(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.IMAGE_RESPONSE:
      authenticateBussiness.onImageResponse(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.HELP_CONTENT_RESPONSE:
      authenticateBussiness.onHelpContentResponse(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.NUMBER_OF_ONLINE_USER_RESPONSE:
      authenticateBussiness.onReceiveNumberOfUserOnline(dataPackage);
      break;
      
    case ProtocolConstants.ResponseHeader.EXCHANGE_RATE_RESPONSE:
      authenticateBussiness.onExchangeRateResponse(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHANGE_MONEY_TO_GOLD_SUCCESS_RESPONSE:
      authenticateBussiness.onChangMoneyToGoldSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHANGE_MONEY_TO_GOLD_FAIL_RESPONSE:
      authenticateBussiness.onChangMoneyToGoldFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.NOTIFICATION_MESSAGE_RESPONSE:
      authenticateBussiness.onReceiveNotificationMessage(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.DISABLE_GAME_AND_APP_CONFIG_RESPONSE:
      authenticateBussiness.onReceiveConfigToDisableGameAndApp(dataPackage);
      break;

    /** Authenticate */
    case ProtocolConstants.ResponseHeader.SALT_REPONSE:
      authenticateBussiness.onSaltResponse(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.AUTHENTICATE_SUCCESS_RESPONSE:
      authenticateBussiness.onAuthenticateSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.AUTHENTICATE_FAIL_RESPONSE:
      authenticateBussiness.onAuthenticateFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHAT_SERVICE_INFO_RESPONSE:
      authenticateBussiness.onChatServiceInfoReceive(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.APP_SERVICE_INFO_RESPONSE:
      authenticateBussiness.onAppServiceInfoReceive(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.REGISTER_SUCCESS_RESPONSE:
      authenticateBussiness.onRegisterSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.REGISTER_FAIL_RESPONSE:
      authenticateBussiness.onRegisterFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.USER_INFO_REPONSE:
      authenticateBussiness.onReceiveUserInfo(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHANGE_USER_INFO_SUCCESS_RESPONSE:
      authenticateBussiness.onChangeUserInfoSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHANGE_USER_INFO_FAIL_RESPONSE:
      authenticateBussiness.onChangeUserInfoFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.USER_MONEY_CHANGE_NOTIFY_RESPONSE:
      authenticateBussiness.onMoneyChange(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHANGE_PASSWORD_SUCCESS_RESPONSE:
      authenticateBussiness.onChangePasswordSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHANGE_PASSWORD_FAIL_RESPONSE:
      authenticateBussiness.onChangePasswordFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.AUTO_PLUS_GOLD_NOTIFY_RESPONSE:
      authenticateBussiness.onReceiveAutoPlusGoldMessage(dataPackage);
      break;

    /** Update GameService list */
    case ProtocolConstants.ResponseHeader.GAME_SERVICE_LIST_RESPONSE:
      authenticateBussiness.onGameServiceList(dataPackage);
      break;
      
    /** Ranking bussiness */
    case ProtocolConstants.ResponseHeader.RANKING_GET_TOP_LEVEL_RESPONSE:
      rankingBussiness.onGetTopLevel(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.RANKING_GET_TOP_RISKEST_RESPONSE:
      rankingBussiness.onGetTopRickest(dataPackage);
      break;
      
    /** Avatar bussiness */
    case ProtocolConstants.ResponseHeader.GET_AVATAR_BY_ID_RESPONSE:
      avatarBussiness.onReceiveAvatar(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.AVATAR_NOT_FOUND_RESPONSE:
      avatarBussiness.onAvatarNotFounḍ(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.GET_AVATAR_BY_CATEGORY_RESPONSE:
      avatarBussiness.onReceivedAvatarListByCategory(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.AVATAR_CATEGORY_LIST_RESPONSE:
      avatarBussiness.onReceivedCategoryList(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHANGE_AVATAR_SUCCESS_RESPONSE:
      avatarBussiness.onChangeAvatarSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CHANGE_AVATAR_FAIL_RESPONSE:
      avatarBussiness.onChangeAvatarFail(dataPackage);
      break;
      
    /** Friend function */
    case ProtocolConstants.ResponseHeader.ADD_FRIEND_NOTIFY_RESPONSE:
      friendBussiness.onAddFriendNotify(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.ADD_FRIEND_SUCCESS_RESPONSE:
      friendBussiness.onAddFriendSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.ADD_FRIEND_FAIL_USER_DENY_RESPONSE:
      friendBussiness.onAddFriendFailUserDeny(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.ADD_FRIEND_FAIL_USER_NOT_EXIST_RESPONSE:
      friendBussiness.onAddFriendFailUserNotExist(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.FRIEND_LIST_RESPONSE:
      friendBussiness.onGetFriendList(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.ALREADY_FRIEND_RESPONSE:
      friendBussiness.onAlreadyFriend(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.REMOVE_FRIEND_SUCCESS_RESPONSE:
      friendBussiness.onRemoveFriendSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.REMOVE_FRIEND_FAIL_RESPONSE:
      friendBussiness.onRemoveFriendFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.TRANSFER_MONEY_SUCCESS_RESPONSE:
      friendBussiness.onTransferMoneySuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.TRANSFER_MONEY_FAIL_RESPONSE:
      friendBussiness.onTransferMoneyFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.RECEIVER_MONEY_SUCCESS_RESPONSE:
      friendBussiness.onReceivedMoneySuccess(dataPackage);
      break;

    /*Messaging function*/
    case ProtocolConstants.ResponseHeader.MESSAGE_LIST_RESPOSNE:
    case ProtocolConstants.ResponseHeader.MESSAGE_DELETE_ALL_RESPONSE:
    case ProtocolConstants.ResponseHeader.MESSAGE_DELETE_READ_RESPONSE:
    case ProtocolConstants.ResponseHeader.MESSAGE_DELETE_RESPONSE:
      ((FrmMessage)FrmMessage.instance()).onRecieveData(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.MESSAGE_UNREAD_COUNT_RESPONSE:
      ((FrmMangXH)FrmMangXH.instance()).setUnreadMessage(dataPackage.nextInt());
      break;
    case ProtocolConstants.ResponseHeader.SEND_MESSAGE_SUCCESS:
      Global.alert.showAlert(this, GameAlert.OK_TYPE, "Gửi tin nhắn thành công");
      break;
    case ProtocolConstants.ResponseHeader.SEND_MESSAGE_FAIL:
      Global.alert.showAlert(this, GameAlert.OK_TYPE, "Gửi tin nhắn thất bại");
      break;
      
    /*Subscription function*/
    case ProtocolConstants.ResponseHeader.SUBSCRIPTION_RESPONSE:
    	subscriptionBussiness.onReceiveSubscription(dataPackage);
    	break;
    default:
      System.err.println("AuthenticateHandle : Receive unknown message header " + header);
      break;
    }
  }
}
